<?php
/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.     *
 *                                                                    *
 **********************************************************************/

/**
 * Ageset entity manager.
 *
 * @version $Id: LinksManager.php,v 1.4 2008/09/22 13:32:46 sdobrovol Exp $
 */
class LinksManager extends ORMManager {
	function getLinks(&$dialog, $where_user = '') {
		if ($dialog->limit_to_page){
			$countpage = $dialog->limit_to_page; 
		} else {
			$countpage = $GLOBALS['options'] ['search'] ['limit_to_page'];
		}
		
		$min = $countpage * $dialog->page;
		
		$links = new LinksDB ( );
		
		$where = array ( );
		if ($dialog->option_id) {
			array_push ( $where, 'option_id = ' . $dialog->option_id );
		}
		
		if (! $dialog->all_links)
			array_push ( $where, 'active' );
		
		if (count ( $where )) {
			$where_add .= ' where ' . join ( ' and ', $where );
		}
		
		if ($where_user)
			$where_add = $where_user;
		
		if ($dialog->no_limit)
			$limit = ""; else
			$limit = "limit $min, $countpage";
		
		$qs = "select SQL_CALC_FOUND_ROWS * from " . $links->__table . " $where_add order by rank desc, link_id desc $limit";
		//print "<!--  $qs  -->";
		

		$dialog->links = $this->getArray ( $qs );
		if (count ( $dialog->links )) {
			$qs = "SELECT FOUND_ROWS() as `count`";
			$dialog->start_links = $min + 1;
			$dialog->count_links = $this->getCount ( $qs );
		}
	
	}
	/**
	 * �������� ���� �� link_id
	 *
	 * @param int $link_id
	 * @return LinksDB object
	 */
	function getLink($link_id) {
		$links_db = new LinksDB ( );
		$links_db->link_id = $link_id;
		$this->find ( $links_db );
		return $links_db;
	}
	
	function editLink(&$dialog) {
		$links_db = new LinksDB ( );
		$links_db->link_id = $dialog->link_id;
		$this->find ( $links_db );
		$this->setProperties ( $links_db, $dialog );
		
		if (! $dialog->active) $links_db->active = 0;
		if (! $dialog->icon) $links_db->icon = '';
		if (! $dialog->name) $links_db->name = '';
		if (! $dialog->title) $links_db->title = '';
		if (! $dialog->email) $links_db->email = '';
		if (! $dialog->url) $links_db->url = '';
		if (! $dialog->url_back) $links_db->url_back = '';
		if (! $dialog->descr) $links_db->descr = '';
		if (! $dialog->rank) $links_db->rank = 0;
		
		$this->update ( $links_db );
	}
	
	function addLink(&$dialog) {
		$links_db = new LinksDB ( );
		$this->setProperties( $links_db, $dialog );
		$links_db->check_not_array = array ('add_date' );
		$links_db->add_date = 'now()';
		$this->insert ( $links_db );
		return $links_db->link_id;
	}
	
	function deleteLink($link_id) {
		$links_db = new LinksDB ( );
		$links_db->link_id = $link_id;
		$this->remove ( $links_db );
	}
	
	public function getRubricsName($option_id) {
		$qs = "SELECT name as count FROM links_tree_names where option_id=$option_id and lang_id=" . $_SESSION ['lang_id'];
		return $this->getCount ( $qs );
	}
	
	public function getAllRubrics() {
		$qs = "SELECT * FROM links_tree_names where lang_id=" . $_SESSION ['lang_id'];
		return $this->getAssArray ( $qs, 'option_id', 'name', 360 );
	}
}

?>